package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.service.impl.UserServiceImpl;
import com.zzyl.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;
    /**
     * 用户添加
     */
    @PutMapping
    public ResponseResult adduser(@RequestBody UserDto user){
        userService.adduser(user);
        return ResponseResult.success(true);
    }
    /**
     * 用户分页
     */
    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult<PageResponse<UserVo>> pageusers(@RequestBody UserDto user,@PathVariable Integer pageNum,@PathVariable Integer pageSize){
        PageResponse<UserVo> userVo = userService.findUserPage(user,pageNum,pageSize);
        return ResponseResult.success(userVo);
    }

    /**
     * 用户列表查询
     * @param userDto
     * @return
     */
    @PutMapping("/list")
    public ResponseResult<UserVo> getlistuser(@RequestBody UserDto userDto){
        List<UserVo> userVoList = userService.getlistuser(userDto);
        return ResponseResult.success(userVoList);
    }

    /**
     * 修改用户
     * @param userDto
     * @return
     */
    @PatchMapping
    public ResponseResult<Boolean> updateUser(@RequestBody UserDto userDto) {
        Boolean flag = userService.updateUser(userDto);
        return ResponseResult.success(flag);
    }

    /**
     * 删除用户
     * @param userId
     * @return
     */
    @DeleteMapping("/remove/{userId}")
    public ResponseResult remove(@PathVariable Long userId) {
        return ResponseResult.success(userService.deleteUserById(userId));
    }

    /**
     * 启用禁用
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/is-enable/{id}/{status}")
    public ResponseResult isEnable(@PathVariable(name = "id") Long id,@PathVariable(name = "status")String status){
        userService.isEnable(id,status);
        return ResponseResult.success();
    }

}
